#include "stdafx.h"
#include "NagiosPerf.h"
#include "Utils.h"
#include "Prefs.h"

#define BUFFER_SIZE   5120
NagiosPerf::NagiosPerf(void)
{
	mData = new char[BUFFER_SIZE];
	memset(mData,0,BUFFER_SIZE);
	mLast = NULL;
	line_no =0;
	mType =0;
}

NagiosPerf::~NagiosPerf(void)
{
	if( mData ) delete mData;
}
int NagiosPerf::Load(FILE * fp)
{
	memset(mData,0,BUFFER_SIZE);
	
	while ( fgets(mData,BUFFER_SIZE,fp)!=NULL ){
		line_no ++;
		mLast = NULL;
	    mHead = Utils::Trim(mData);
		if( mHead[0]!=0 ){
			if( mType==0 ) CheckType();
			switch(mType){
			case 1: 
				if( ReadServiceFields() ) return 1;
				break;
			case 2:
				if( ReadHostFields() ) return 1;
				break;
			default:
				break;
			}
		}
    	memset(mData,0,BUFFER_SIZE);
	}
	return 0;
}
int NagiosPerf::CheckType()
{
	if( mHead==NULL ) return 0;
	if( strstr(mHead,"SERVICEPERFDATA::") ){
		mType =1;
	}
	if( strstr(mHead,"HOSTPERFDATA::") ) {
		mType=2;
	}
	return 1;
}
char * NagiosPerf::Read(const char *field)
{
	if( mLast ) mLast[0]=mChar;
	mLast = NULL;
	char * tmp = strstr(mHead,field);
	if( tmp ){
		tmp +=strlen(field);
		if( tmp[0]=='"' ){
			tmp++;
			mLast = tmp;
			while( (mLast[0]!=0) && ( mLast[0]!='"') )	mLast++;
			mChar ='"';
		} else {
		   mLast = tmp;
           while( (mLast[0]!=0) && (!isspace(mLast[0])) ) mLast ++;
		   mChar = mLast[0];
		} 
		mLast[0]=0;
		return tmp;
	}
	return NULL;
}
int NagiosPerf::ReadHostFields()
{
//DATATYPE::HOSTPERFDATA  TIMET::1512040855       HOSTNAME::1.111 HOSTPERFDATA::"rta=0.492000ms;3000.000000;5000.000000;0.000000 pl=0%;80;100;0"
// HOSTCHECKCOMMAND::check-host-alive      HOSTSTATE::UP   HOSTSTATETYPE::HARD

	char * tmp2;
	char * tmp = Read("TIMET::");
	if( tmp ) mTime = atol(tmp);
	if( (tmp = Read("HOSTNAME::"))==NULL ) return 0;
    strcpy(mHost,tmp);

	mService[0]=0;

	if( (tmp = Read("HOSTCHECKCOMMAND::")) == NULL ) return 0;
	tmp2 = strchr(tmp,'!');
	if( tmp2 ) tmp2[0]=0;
	strcpy(mCommand,tmp);
	if( tmp2 ) tmp2[0]='!';
//	tmp2 = strstr(mCommand,"check_");
//	if( !tmp2 ) tmp2 = strstr(mCommand,"check-");
//	if( tmp2 ) memmove(tmp2,tmp2+6,strlen(tmp2)-5);

	if( (tmp = Read("HOSTSTATE::")) == NULL ) return 0;
	if( strcmpi(tmp,"UP")==0 ) mState = 1;
	else mState = 3;

	if( (mPerfData = Read("HOSTPERFDATA::")) == NULL ) return 0;
	if( mPerfData[0]==0 ) return 0;
	return 1;
}
int NagiosPerf::ReadServiceFields()
{
//DATATYPE::SERVICEPERFDATA       TIMET::1513350375       HOSTNAME::1.115 SERVICEDESC::"check_nflow"
//SERVICEPERFDATA::"eth0.in=0.04;1.00;2.00; eth0.out=0.02;1.00;2.00; eth0.all=0.06;1.00;2.00; eth1.in=0.01;10.00;20.00; eth1.out=0.02;10.00;20.00; eth1.all=0.03;10.00;20.00;"        
//SERVICECHECKCOMMAND::check_nflow!1,2!10,20!        HOSTSTATE::UP   HOSTSTATETYPE::HARD     SERVICESTATE::OK        SERVICESTATETYPE::HARD
	char * tmp2;
	char * tmp = Read("TIMET::");
	if( tmp ) mTime = atol(tmp);
	if( (tmp = Read("HOSTNAME::"))==NULL ) return 0;	
    strcpy(mHost,tmp);

	if( (tmp = Read("SERVICEDESC::")) == NULL ) return 0;
	strcpy(mService,tmp);

	if( (tmp = Read("SERVICECHECKCOMMAND::")) == NULL ) return 0;
	tmp2 = strchr(tmp,'!');
	if( tmp2 ) tmp2[0]=0;
	strcpy(mCommand,tmp);
	if( tmp2 ) tmp2[0]='!';

	if( strcmp(mService,mCommand) )	strcpy(mCommand,mService);

	tmp2 = strstr(mService,"check_");
	if( !tmp2 ) tmp2 = strstr(mService,"check-");
	if( tmp2 ) memmove(tmp2,tmp2+6,strlen(tmp2)-5);

	//	tmp2 = strstr(mCommand,"check_");
//	if( !tmp2 ) tmp2 = strstr(mCommand,"check-");
//	if( tmp2 ) memmove(tmp2,tmp2+6,strlen(tmp2)-5);

	if( (tmp = Read("SERVICESTATE::")) == NULL ) return 0;
	if( strcmpi(tmp,"OK")==0 ) mState = 1;
	else if( strcmpi(tmp,"Warning") ==0 ) mState = 2;
	else if( strcmpi(tmp,"Critical") ==0 ) mState =3;
	else mState = 4;

	if( (mPerfData = Read("SERVICEPERFDATA::")) == NULL ) return 0;
	if( mPerfData[0]==0 ) return 0;
	return 1;
}
int NagiosPerf::toJson(KeyPairOutter &out)
{
  //"\"@timestamp\": \"$stime+0800\",\"@version\":\"5001\",\"ip\":\"$LocalHost\",\"src_ip\":\"$LocalHost\",\"severity\":$Severity,\"msg\":\"$msg\""
    char tmp[128];
	memset(tmp,0,128);
	Utils::formattime(tmp,mTime);
	out.Put("{ \"@timestamp\":\"%s+0800\",\"name\":\"%s\",\"severity\":%d",tmp,mHost,mState);
	if( gPrefs->mP1[0] ) out.Put(",\"idc\":\"%s\"",gPrefs->mP1);
	char * t = mPerfData;
	char * t2;
	int k;
	while(t){
		//name
		t2 = strchr(t,'=');
		if( !t2 ) break;
		t2[0]=0;
		k = Utils::json_escape_str_trim(tmp,t);
		if( k==0 ) return -1;
		tmp[k]=0;
		if( mService[0] ) out.Put(",\"%s.%s\":",mService,tmp);
		else out.Put(",\"%s\":",tmp);
		t2[0]='=';
		
		t2++;
		while(isspace(t2[0])) t2++;
        k=0;
		while(t2[0]){
			if( isdigit(t2[0]) ) tmp[k++]=t2[0];
			else if( t2[0]=='.' ) tmp[k++]=t2[0];
			else break;
			t2++;
		}
		tmp[k]=0;
		if( strlen(tmp)<1 ) return -2;
		out.PutQ(tmp);
        t = t2;
		while( t[0] && (!isspace(t[0])) ) t++;
	}
	out.PutQ(" }");
	return 1;
}